package com.example.config;

import cn.hutool.db.meta.JdbcType;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.math.BigDecimal;
import java.sql.Clob;
import java.util.HashMap;
import java.util.Map;

public class Jdbc2Java {

    @AllArgsConstructor
    @Getter
    public enum Jdbc2JavaEnum {
        //字符串
        VARCHAR(JdbcType.VARCHAR, String.class, "String", "String", null, "字符串类型"),
        //长整数
        BIGINT(JdbcType.BIGINT, Long.class, "Long", "long", null, "长整数类型"),
        //整数
        INTEGER(JdbcType.INTEGER, Integer.class, "Integer", "int", null, "整数类型"),
        //高精度带小数
        DECIMAL(JdbcType.DECIMAL, BigDecimal.class, "BigDecimal", "BigDecimal", "java.math.BigDecimal", "高精度数字类型"),
        //布尔BOOLEAN
        BOOLEAN(JdbcType.BOOLEAN, Boolean.class, "Boolean", "boolean", null, "布尔类型"),
        //布尔BIT
        BIT(JdbcType.BIT, Boolean.class, "Boolean", "boolean", null, "布尔类型"),
        //大文本
        CLOB(JdbcType.CLOB, Clob.class, "Clob", "Clob", "java.sql.Clob", "CLOB类型"),


        ;


        private static final Map<Object, Jdbc2JavaEnum> JDBC_TO_JAVA_MAP;

        static {
            JDBC_TO_JAVA_MAP = new HashMap<>();
            for (Jdbc2JavaEnum mapping : Jdbc2JavaEnum.values()) {
                JDBC_TO_JAVA_MAP.put(mapping.getJdbcType(), mapping);
                JDBC_TO_JAVA_MAP.put(mapping.getJavaType(), mapping);
            }
        }

        private JdbcType jdbcType;
        private Class javaType;
        //可空java数据类型名
        private String javaTypeNull;
        //不可空java数据类型名
        private String javaTypeNotNull;
        private String importClass;
        private String describe;

        /**
         * @param code JdbcType的code
         * @return
         */
        public static Jdbc2JavaEnum getJdbc2JavaEnum(int code) {
            return JDBC_TO_JAVA_MAP.get(JdbcType.valueOf(code));
        }

        public static Jdbc2JavaEnum getJdbc2JavaEnum(JdbcType type) {
            return JDBC_TO_JAVA_MAP.get(type);
        }

    }
}
